local jingwan = fk.CreateSkill {
  name = "hanqing__jingwan",
}

Fk:loadTranslationTable{
  ["hanqing__jingwan"] = "靖晚",
  [":hanqing__jingwan"] = "其他角色的回合结束时，若其体力值和手牌数均大于你，其可以将一张手牌当【杀】对你使用。每轮结束时，"..
  "本轮对你造成过伤害的角色各摸一张牌，然后你摸等于这些角色数的牌。若你对这些角色均发动过〖韵致〗，你回复1点体力。",

  ["#hanqing__jingwan-slash"] = "靖晚：你可以将一张手牌当【杀】对 %src 使用",

  ["$hanqing__jingwan1"] = "相逢情便深，恨不相逢早。",
  ["$hanqing__jingwan2"] = "识尽千千万万人，终不似、伊家好。",
}

jingwan:addEffect(fk.TurnEnd, {
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jingwan.name) and target ~= player and
      target.hp > player.hp and target:getHandcardNum() > player:getHandcardNum() and
      target:canUseTo(Fk:cloneCard("slash"), player, { bypass_distances = true, bypass_times = true })
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local use = room:askToUseVirtualCard(target, {
      name = "slash",
      skill_name = jingwan.name,
      prompt = "#hanqing__jingwan-slash:"..player.id,
      cancelable = true,
      extra_data = {
        exclusive_targets = { player.id },
      },
      card_filter = {
        n = 1,
        cards = target:getHandlyIds(),
      },
      skip = true,
    })
    if use then
      event:setCostData(self, {extra_data = use})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useCard(event:getCostData(self).extra_data)
  end,
})

jingwan:addEffect(fk.RoundEnd, {
  anim_type = "drawcard",
  can_trigger = function(self, event, player, data)
    return player:hasSkill(jingwan.name) and
      #player.room.logic:getActualDamageEvents(1, function (e)
        return e.data.to == player
      end, Player.HistoryRound) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = {}
    room.logic:getActualDamageEvents(1, function (e)
      local damage = e.data
      if damage.to == player and damage.from then
        table.insertIfNeed(tos, damage.from)
      end
    end, Player.HistoryRound)
    room:sortByAction(tos)
    for _, p in ipairs(tos) do
      if not p.dead then
        p:drawCards(1, jingwan.name)
      end
    end
    if not player.dead then
      player:drawCards(#tos, jingwan.name)
    end
    if table.every(tos, function (p)
      return table.contains(player:getTableMark("hanqing__yunzhi"), p.id)
    end) and not player.dead then
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = jingwan.name,
      }
    end
  end,
})

return jingwan
